package com.freak.neteasecloudmusic.modules.mine;

import android.util.Log;

import com.freak.httphelper.ApiCallback;
import com.freak.httphelper.HttpMethods;
import com.freak.httphelper.RxPresenter;
import com.freak.httphelper.SubscriberCallBack;
import com.freak.neteasecloudmusic.app.api.ApiService;
import com.freak.neteasecloudmusic.commom.constants.Constants;
import com.freak.neteasecloudmusic.modules.mine.entity.MinePlayListEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MinePurchasedEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MineRadioStationEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MineRecordEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MineUserDetailEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MineUserInfoEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.PersonFmEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.RecommendRadioStationEntity;
import com.freak.neteasecloudmusic.net.log.LogUtil;
import com.freak.neteasecloudmusic.utils.ToastUtil;
import com.google.gson.Gson;

import io.reactivex.Observable;

public class MinePresenter extends RxPresenter<MineContract.View> implements MineContract.Presenter {
    private ApiService apiService = HttpMethods.getInstance().create(ApiService.class);

    @Override
    public void getUserDetail(String uid) {
        Observable<MineUserDetailEntity> observable = apiService.getUserDetail(uid);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<MineUserDetailEntity>() {
            @Override
            public void onSuccess(MineUserDetailEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getUserDetailSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getUserLikeList(String uid) {
        Observable<Object> observable = apiService.getUserLikeList(uid);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<Object>() {
            @Override
            public void onSuccess(Object model) {
                LogUtil.e(new Gson().toJson(model));
//                if (model.getCode() == Constants.SUCCESS_CODE) {
//                    if (mView==null){
//                        LogUtil.e("mView 为空");
//                    }else {
//                        mView.getUserInfoSuccess(model);
//                    }
//                } else {
//                    ToastUtil.shortShow("获取数据失败");
//                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getUserInfo() {
        Observable<MineUserInfoEntity> observable = apiService.getUserInfo();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<MineUserInfoEntity>() {
            @Override
            public void onSuccess(MineUserInfoEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getUserInfoSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getPersonFm() {
        Observable<PersonFmEntity> observable = apiService.getPersonFm();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<PersonFmEntity>() {
            @Override
            public void onSuccess(PersonFmEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getPersonFmSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getDjProgram() {
        Observable<RecommendRadioStationEntity> observable = apiService.getDjProgram();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<RecommendRadioStationEntity>() {
            @Override
            public void onSuccess(RecommendRadioStationEntity model) {
                LogUtil.e(new Gson().toJson(model));
//                if (model.getCode() == Constants.SUCCESS_CODE) {
//                    if (mView==null){
//                        LogUtil.e("mView 为空");
//                    }else {
//                        mView.getUserInfoSuccess(model);
//                    }
//                } else {
//                    ToastUtil.shortShow("获取数据失败");
//                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getRecommend() {
        Observable<Object> observable = apiService.getRecommend();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<Object>() {
            @Override
            public void onSuccess(Object model) {
                LogUtil.e(new Gson().toJson(model));
//                if (model.getCode() == Constants.SUCCESS_CODE) {
//                    if (mView==null){
//                        LogUtil.e("mView 为空");
//                    }else {
//                        mView.getUserInfoSuccess(model);
//                    }
//                } else {
//                    ToastUtil.shortShow("获取数据失败");
//                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getPrivateContent() {
        Observable<Object> observable = apiService.getPrivateContent();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<Object>() {
            @Override
            public void onSuccess(Object model) {
                LogUtil.e(new Gson().toJson(model));
//                if (model.getCode() == Constants.SUCCESS_CODE) {
//                    if (mView==null){
//                        LogUtil.e("mView 为空");
//                    }else {
//                        mView.getUserInfoSuccess(model);
//                    }
//                } else {
//                    ToastUtil.shortShow("获取数据失败");
//                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getPlaylist(String uid) {
        Observable<MinePlayListEntity> observable = apiService.getPlaylist(uid);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<MinePlayListEntity>() {
            @Override
            public void onSuccess(MinePlayListEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getPlaylistSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getUserRadioStation(String uid) {
        Observable<MineRadioStationEntity> observable = apiService.getUserRadioStation(uid);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<MineRadioStationEntity>() {
            @Override
            public void onSuccess(MineRadioStationEntity model) {

                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getUserRadioStationSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getPurchased(int limit) {
        Observable<MinePurchasedEntity> observable = apiService.getPurchased(limit);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<MinePurchasedEntity>() {
            @Override
            public void onSuccess(MinePurchasedEntity model) {

                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getPurchasedSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

    @Override
    public void getRecord(String uid, String type) {
        Observable<MineRecordEntity> observable = apiService.getRecord(uid,type);
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<MineRecordEntity>() {
            @Override
            public void onSuccess(MineRecordEntity model) {

                Log.e("TAG",new Gson().toJson(model));
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    if (mView==null){
                        LogUtil.e("mView 为空");
                    }else {
                        mView.getRecordSuccess(model);
                    }
                } else {
                    ToastUtil.shortShow("获取数据失败");
                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

}
